package cc.vv.baselibrary.view.columnchart

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import cc.vv.baselibrary.R
import cc.vv.baselibrary.view.BCNoScrollHorizontalRecyclerView
import cc.vv.baselibrary.view.columnchart.adapter.SingleViewHorizontalViewAdapter
import cc.vv.baselibrary.view.columnchart.bean.ShowPopwinDataObj
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 *
 * Created by sun.li on 2018/8/4.
 * @author sun.li
 */
class SingleViewHorizontal : LinearLayout {

    var mContext: Context? = null

    var rv_svhv_recycler_view: BCNoScrollHorizontalRecyclerView? = null

    var view_svh_line: View? = null

    var mAdapter: SingleViewHorizontalViewAdapter? = null

    constructor(context: Context) : super(context) {
        this.mContext = context
        initView()
        initViewEvent()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        this.mContext = context
        initView()
        initViewEvent()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        this.mContext = context
        initView()
        initViewEvent()
    }

    fun initView() {
        if (mContext == null) {
            return
        }
        val view = LayoutInflater.from(mContext).inflate(R.layout.view_single_view_horizontal, null)
        rv_svhv_recycler_view = view.findViewById(R.id.rv_svhv_recycler_view)
        view_svh_line = view.findViewById(R.id.view_svh_line)

        this.addView(view)
    }

    fun initViewEvent() {
        val data = ArrayList<ShowPopwinDataObj>()
//        for (i in 30..85) {
//            val obj = ShowPopwinDataObj()
//            obj.valueData = i.toDouble()
//            obj.timeStr = BaseTimeUtil.getNowDateString(BaseTimeUtil.DATE_FORMAT_1)
//            data.add(0, obj)
//        }
        mAdapter = SingleViewHorizontalViewAdapter(R.layout.day_graph_item_layout, data)
        rv_svhv_recycler_view?.adapter = mAdapter
        rv_svhv_recycler_view?.post(Runnable {
            rv_svhv_recycler_view?.scrollToPosition(data.size - 1)
        })
    }

    fun setViewData(data: ArrayList<ShowPopwinDataObj>?) {
        if (null == data) return
        mAdapter!!.setNewData(data)
    }

    /**
     * 控制竖线显示与隐藏
     */
    fun refrushLine(position: Int?) {
        val showLine = mAdapter!!.data[position!!].isShowLine
        mAdapter!!.data[position].isShowLine = !showLine
        mAdapter!!.notifyItemChanged(position)
    }

    /**
     * 获取最顶部线的坐标
     */
    fun getTopLineYPosition(): Int? {
        val intArray = IntArray(2)
        rv_svhv_recycler_view!!.getLocationInWindow(intArray)
        return intArray[1]
    }

    /**
     * 设置条目点击事件监听
     */
    fun setOnItemlickListener(onItemClickListener: BaseQuickAdapter.OnItemClickListener?) {
        mAdapter!!.onItemClickListener = onItemClickListener
    }
}